package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ShopItemListAdapter;
import com.iexbuy.ihg.adapter.ShopsAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.BusinessType;
import com.iexbuy.ihg.entity.GoodCategoryObject;
import com.iexbuy.ihg.entity.Shop;
import com.iexbuy.ihg.entity.ShopCenter;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.entity.ShopItemListObjectResponse;
import com.iexbuy.ihg.entity.ShopListResponse;
import com.iexbuy.ihg.entity.ShopResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class ShopCenterShopSearchResultFragment extends BaseFragment {

	private static ShopCenterShopSearchResultFragment homeFragmentInstent;
	private final static String ALL_IND = "-100";

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.categorySelectLayout)
	private LinearLayout categorySelectLayout;
	@ViewInject(R.id.categoryLayout)
	private LinearLayout categoryLayout;
	@ViewInject(R.id.categoryListView)
	private ListView categoryListView;
	@ViewInject(R.id.categoryBtn)
	private Button categoryBtn;
	@ViewInject(R.id.searchfailLayout)
	private RelativeLayout searchfailLayout;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.adItemList)

	private List<ShopItemListObject> shopItemListObjects = new ArrayList<ShopItemListObject>();
	private int pageNo = 0;
	private boolean hasNextPage;
	private ShopsAdapter shopsAdapter;
	private List<Shop> shops = new ArrayList<Shop>();

	private static ShopCenter shopCenter;
	//云购

	private static GoodCategoryObject selectGoodCategory;

	private static String selectedBusinessTypeCode;
	private static String selectedBusinessTypeName = "类别";

	//热门分类
	private List<BusinessType> businessTypes = new ArrayList<BusinessType>();


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
								 Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_shopcenter_shop_search_result, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_shop_detail));

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshDate();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				loadMore();
			}
		});
		msgListRefreshLayout.autoRefresh();
		businessTypes.clear();
		if(ShopCenterDetailFragment.getBusinessTypes()!=null){
			businessTypes.addAll(ShopCenterDetailFragment.getBusinessTypes());
		}


		categoryBtn.setText("全部");
		initHotCategoryView();
	}

	//刷新整个页面数据
	private void refreshDate() {
		pageNo = 0;
		getSearchShops();
	}

	/**
	 * 查询列表
	 */
	private void getSearchShops() {
		searchfailLayout.setVisibility(View.GONE);
		HashMap<String, String> paramMap = new HashMap<String, String>();

		if (StringUtil.isNotEmpty(selectedBusinessTypeCode)) {
			paramMap.put("category", selectedBusinessTypeCode);
		}
		if (shopCenter != null) {
			paramMap.put("shopCenterId", shopCenter.getId());
		}
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_SHOPLIST_BY_SHOP_CENTERS_ID, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						ShopListResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopListResponse.class);
						hasNextPage = rsp.getBody().isHasNext();
						if (pageNo == 0) {
							shops.clear();
						}
						shops.addAll(rsp.getBody().getData());
						Log.d("aa",shops+"");
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						searchfailLayout.setVisibility(View.VISIBLE);
					}
				});
	}

	private void initListView() {
		if(shops.size()==0){
			searchfailLayout.setVisibility(View.VISIBLE);
		}
		if (shopsAdapter == null) {
			shopsAdapter = new ShopsAdapter(mBaseActivity, shops);
			listView.setAdapter(shopsAdapter);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					ShopDetailFragment.setShop(shops.get(i));
					CommUtil.openSingleFragmentActivity(mBaseActivity, ShopDetailFragment.class);
				}
			});
		}
	}

	private void loadMore() {
		pageNo ++;
		getSearchShops();
	}

	private void initHotCategoryView() {
		businessTypes.add(new BusinessType("全部类别", ALL_IND));

		Collections.sort(businessTypes, new Comparator<BusinessType>() {
			@Override
			public int compare(BusinessType businessType, BusinessType t1) {
				return businessType.getCode().compareTo(t1.getCode());
			}
		});

		categoryListView.setAdapter(new BaseAdapter() {
			@Override
			public int getCount() {
				return businessTypes.size();
			}

			@Override
			public Object getItem(int i) {
				return businessTypes.get(i);
			}

			@Override
			public long getItemId(int i) {
				return 0;
			}

			@Override
			public View getView(int i, View view, ViewGroup viewGroup) {
				if (view == null) {
					view = LayoutInflater.from(mBaseActivity).inflate(R.layout.list_item_single_text, null);
				}
				((TextView) view.findViewById(R.id.itemName)).setText(businessTypes.get(i).getName());
				return view;
			}
		});
		categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				if (ALL_IND.equalsIgnoreCase(businessTypes.get(i).getCode())) {
					selectedBusinessTypeCode = "";
					categoryBtn.setText("全部类别");
				} else {
					selectedBusinessTypeCode = businessTypes.get(i).getCode();
					categoryBtn.setText(businessTypes.get(i).getName());
				}
				hideCityCategoryLayout();
				getSearchShops();
			}
		});
	}

	private void hideCityCategoryLayout() {
		categoryLayout.setVisibility(View.GONE);
	}

	@OnClick(R.id.categorySelectLayout)
	public void categorySelectLayoutClick(View view) {
		if (categoryLayout.isShown()){
			categoryLayout.setVisibility(View.GONE);
		}else{
			categoryLayout.setVisibility(View.VISIBLE);
		}

	}
	@OnClick(R.id.categoryLayoutMask)
	public void categoryLayoutMaskClick(View view) {
		categoryLayout.setVisibility(View.GONE);
	}

	public static void setSelectedBusinessTypeCode(String selectedBusinessTypeCode) {
		ShopCenterShopSearchResultFragment.selectedBusinessTypeCode = selectedBusinessTypeCode;
	}
	public static void setSelectedBusinessTypeName(String selectedBusinessTypeName) {
		ShopCenterShopSearchResultFragment.selectedBusinessTypeName = selectedBusinessTypeName;
	}

	public static void setShopCenter(ShopCenter shopCenter) {
		ShopCenterShopSearchResultFragment.shopCenter = shopCenter;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static ShopCenterShopSearchResultFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
